package com.miniapp.task;

import com.miniapp.service.BaseFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 临时文件清理定时任务
 *
 * <p>定期清理过期的临时文件，释放存储空间
 * <p>默认每小时执行一次
 *
 * @author yourName
 * @date 2025-09-06
 */
@Component
public class TempFileCleanupTask {

    private static final Logger log = LoggerFactory.getLogger(TempFileCleanupTask.class);

    @Autowired
    private BaseFileService baseFileService;

    /**
     * 定时清理过期临时文件
     *
     * <p>cron表达式：0 0 * * * ? 表示每小时执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void cleanExpiredTemporaryFiles() {
        log.info("开始清理过期临时文件...");
        try {
            int deleteCount = baseFileService.cleanExpiredTemporaryFiles();
            log.info("临时文件清理完成，共清理过期文件: {} 个", deleteCount);
        } catch (Exception e) {
            log.error("临时文件清理任务执行失败", e);
        }
    }
}
